<template>
  <div class="xtx-goods-page">
    <div class="container">
      <!-- 商品信息 -->
      <div class="goods-info" v-if="goods && goods.brand">
        <div class="media">
          <!-- 图片预览区 -->
          <XtxImageView :image-list="goods.mainPictures" />
          <!-- 统计数量 -->
          <GoodSale :good="goods" />
        </div>
        <div class="spec">
          <!-- 商品信息区 -->
          <GoodInfo :good="goods" />
          <!-- sku组件 -->
          <XtxGoodSku :goods="goods" @change="skuChange" />
          <!-- 数据组件 -->
          <XtxNewNumbox :min="1" :max="5" v-model="num"/>
          <!-- 按钮组件 -->
          <XtxButton type="primary" style="margin-top: 20px" @click="insertCart"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品详情 -->
          <goods-detail :goods='goods'/>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <goods-hot :type="1"/>
          <goods-hot :type="2"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodSale from './components/good-sale.vue'
import GoodInfo from './components/good-info.vue'
import GoodsDetail from './components/good-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import { findGoods } from '@/api/good'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import router from '@/router'
import { MessageTest } from '@/components/Message/index.js'
export default {
  name: 'goods',
  components: {
    GoodSale,
    GoodInfo,
    GoodsDetail,
    GoodsHot
  },
  setup () {
    const goods = ref({})
    const route = useRoute()
    const num = ref(2)
    async function getGoods () {
      const res = await findGoods(route.params.id)
      // console.log(res)
      goods.value = res.data.result
    }

    let skuIdObj = {}
    // sku
    function skuChange (sku) {
      // console.log(sku.skuId)
      skuIdObj = sku
    }
    const store = useStore()
    // 点击购物车
    async function insertCart () {
      // 获取vuex中的token
      const token = store.state.user.profile.token
      // 判断是否有token 如果没有
      if (!token) {
        // 调转到登陆页面 重新登录调转到退出之前的页面
        router.push({
          path: '/login',
          query: {
            redirect: encodeURIComponent(route.fullPath)
          }
        })
      } else {
        // 调取添加购物车的接口
        await store.dispatch('cart/asyncInsertCart', { skuId: skuIdObj.skuId, count: num.value })
        MessageTest({ text: '加入购物车成功', type: 'success' })
      }
    }

    getGoods()
    return {
      goods,
      num,
      insertCart,
      skuChange
    }
  }
}
</script>
<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}

.number-box {
  display: flex;
  align-items: center;
  .label {
    width: 60px;
    color: #999;
    padding-left: 10px;
  }
}
</style>
